/*
 *	User Info JS
 *	@deps JQuery
 *	@author zhangbaitong
 */

$(function () {
	var trackInfo = new TrackInfo();
	$('#save').click(trackInfo.save);
	$('[name="delete"]').click(trackInfo.delete);
	$('#reset').click(trackInfo.reset);
	$('#cancel').click(trackInfo.home);
    $('#trackadd').click(trackInfo.trackadd);
    $('#login').click(trackInfo.login);
    $('#searchreset').click(trackInfo.searchreset);
    $('#goclick').click(trackInfo.goclick);
    $('#prevClick').click(trackInfo.prevclick);
    $('#nextClick').click(trackInfo.nextclick);
    $('#import').click(trackInfo.import);
    $('#date').datetimepicker();
});

function TrackInfo(){
	var self = this;

    this.import = function(){
        window.location.href = '/track/excel/index';
    }

    this.prevclick = function(){
        var gonum = $(this).attr("prev");
        $('#gonum').val(gonum);
        $('#searchform').submit();
    }

    this.nextclick = function(){
        var gonum = $(this).attr("next");
        $('#gonum').val(gonum);
        $('#searchform').submit();
    }

    this.goclick = function(){
        var gonum = $('#gonum').val();
        $('#searchform').submit();

    }
    this.searchreset = function(){
        $('#date').val('');
        $('#from').val('');
        $('#fromkey').val('');
        $('#name').val('');
        $('#area').val('');
        $('#qq').val('');
        $('#tel').val('');
        $('#mail').val('');
        $('#nhtype').val('');
        $('#comname').val('');
        $('#nhscope').val('');
        $('#step').val('');
        $('#note').val('');
        $('#searchform').submit();
    }
    this.login = function(){
        var data = {
            'name': $('#username').val(),
            'passwd': $('#passwd').val()
        };
        self.ajax('/track/index/login', data, function(data){
            if(data.code == 200){
                self.home();    
            }else{
                alert(data.msg);
            }
        });
    }
    this.trackadd = function(){
        var data = {
            'userid': $('#userid').val(),
            'date': $('#trackdate').val(),
            'name': $('#trackname').val(),
            'detail': $('#trackdetail').val()
        };
        self.ajax('/track/index/tracksave', data, function(data){
            if(data.code == 200){
                window.location.reload();
            }else{
                alert(data.msg);
            }
        });
    }

	this.save = function(){
        var data = {
            'id': 	$('#userid').val(),
            'date': $('#date').val(),
            'from': $('#from').val(),
            'fromkey': $('#fromkey').val(),
            'name': $('#name').val(),
            'area': $('#area').val(),
            'qq': $('#qq').val(),
            'tel': $('#tel').val(),
            'mail': $('#mail').val(),
            'nhtype': $('#nhtype').val(),
            'comname': $('#comname').val(),
            'nhscope': $('#nhscope').val(),
            'step': $('#step').val(),
            'note': $('#note').val()
        };
        self.ajax('/track/index/save',data,function(data){
        	alert(data.msg);
        	self.home();
        });
	}

	this.delete = function(){
        var data = {
            'id':$(this).attr("userid"),
        };
        self.ajax('/track/index/delete',data,function(data){
        	// alert(data['msg']);
        	self.home();
        });
	}

	this.reset = function(){
		var id = $('#userid').val();
        if (id) {
            $('#area').val('');
        } else {
            $('#area').val('');
            $('#name').val('');
        }
	}

	this.home = function(){
		window.location.href = '/track/index/list';
	}

	this.ajax = function(url, data, callback){
		$.ajax({
	        url : url,
	        type : "post",
	        async : true,
	        cache : false,
	        data : data,
	        dataType : "json",
	        success : function(recData){
	            callback(recData);
	        },
	        error : function(e){
	            alert(e);
	            console.log('error:', e);
	        }
    	});
	}
}